package com.ggs.rg.ybjb.adminmodule.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.adminmodule.service.TAnnouncementService;
import com.ggs.rg.ybjb.adminmodule.util.ImageUtil;
import com.ggs.rg.ybjb.pojomodule.pojo.TAnnouncement;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.pojomodule.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cxw
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/tacModule")
public class TAnnouncementController {

    @Autowired
    TAnnouncementService tAnnouncementService;


    @Value("${server.port}")
    private String port;

    //添加公告,图片应该先插入数据库,确定插入数据库成功后,再保存到服务
    @PostMapping("/addTAC")
    public JSONObject addTAC(@RequestParam("p1") MultipartFile p1,@RequestParam("text") String text) throws IOException {
        //保存图片到服务器
        String newName = ImageUtil.getNewFileName(p1);
        //将公告信息插入数据库
        TAnnouncement tAnnouncement = new TAnnouncement();
        tAnnouncement.setText(text);
        tAnnouncement.setImage(newName);
        System.out.println(tAnnouncement);
        boolean result = tAnnouncementService.save(tAnnouncement);
        if (result){
            //确定插入数据库成功后,将图片保存到服务器
            ImageUtil.saveFile(newName,p1);
            return CommonUtil.successJson(true);
        }else {
            return CommonUtil.errorJson(ErrorEnum.E_30001);
        }
    }

    //删除公告
    @PostMapping("/deleTAC")
    public JSONObject deleTAC(@RequestBody JSONObject jsonObject) throws IOException {
        String id = jsonObject.getString("id");
        if(StringTools.isNullOrEmpty(id))
            return CommonUtil.errorJson(ErrorEnum.E_90003);
        //判断指定id的公告是否存在
        TAnnouncement tAnnouncement = tAnnouncementService.getById(id);
        if (tAnnouncement == null)
            return CommonUtil.sqlErrorJson("没有查询到指定id的公告");
        else {
            String image = tAnnouncement.getImage();
            //数据库删除信息
           if (tAnnouncementService.removeById(id)){
               //删除原图片
               ImageUtil.deleteFile(image);
               return CommonUtil.successJson(true);
           }else {
               return CommonUtil.sqlErrorJson("sql异常,删除指定id失败");
           }
        }
    }

    //更新公告
    @PostMapping("/updateTAC")
    public JSONObject updateTAC(@RequestParam Integer id,@RequestParam String text,MultipartFile p1) throws IOException {
        //获取请求参数,如果为空直接返回错误提示
        if (id == null || text == null)
            return CommonUtil.errorJson(ErrorEnum.E_90003);
        //判断要删除的id是否存在
        TAnnouncement dataTAnnouncement = tAnnouncementService.getById(id);
        if (dataTAnnouncement == null)
            return CommonUtil.sqlErrorJson("没有查询到指定id的公告");
        //获取数据库中保存的原图片名,
        if(p1 != null){
            String imageName = dataTAnnouncement.getImage();
            //获取图片,并调用工具类重置图片名
            String newImageName = ImageUtil.getNewFileName(p1);
            TAnnouncement tAnnouncement = new TAnnouncement();
            tAnnouncement.setId(id);
            tAnnouncement.setText(text);
            tAnnouncement.setImage(newImageName);
            boolean upResult = tAnnouncementService.updateById(tAnnouncement);
            if (upResult){
                //如果数据库更新成功,将更新图片保存到服务器,删除原图片
                ImageUtil.saveFile(newImageName,p1);
                ImageUtil.deleteFile(imageName);
                return CommonUtil.successJson(true);
            }
        }else {
            String imageName = dataTAnnouncement.getImage();
            TAnnouncement tAnnouncement = new TAnnouncement();
            tAnnouncement.setId(id);
            tAnnouncement.setText(text);
            boolean upResult = tAnnouncementService.updateById(tAnnouncement);
            if (upResult)
                return CommonUtil.successJson(true);
        }
        return CommonUtil.sqlErrorJson("更新公告失败");
    }

    //获取全部公告
    @GetMapping("/getTAC")
    public JSONObject getATC() {
        QueryWrapper<TAnnouncement> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<TAnnouncement> list = tAnnouncementService.list(wrapper);
        for (TAnnouncement tAnnouncement : list) {
            String image = tAnnouncement.getImage();
            String imageAddr = ImageUtil.getImageAddr(image,port);
            tAnnouncement.setImage(imageAddr);
        }
        list.forEach(System.out::print);
        return CommonUtil.successJson(list);
    }

}

